package xiao_jin.fantastic_journey.core.common.item;

import com.google.common.collect.Lists;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.StackReference;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.item.tooltip.TooltipType;
import net.minecraft.screen.slot.Slot;
import net.minecraft.text.Text;
import net.minecraft.util.ClickType;
import net.minecraft.util.Rarity;

import java.util.List;
import java.util.Random;

public class Collar extends Item {
    // TODO

    public Collar() {
        super(new Settings().maxCount(1).rarity(Rarity.EPIC));
    }

    @Override
    public void appendTooltip(ItemStack stack, TooltipContext context, List<Text> tooltip, TooltipType type) {
        super.appendTooltip(stack, context, tooltip, type);
        tooltip.add(getTooltipText());
    }

    @Override
    public boolean onClicked(ItemStack stack, ItemStack otherStack, Slot slot, ClickType clickType, PlayerEntity player, StackReference cursorStackReference) {
        if (clickType != ClickType.RIGHT) return false;
        cursorStackReference.set(Items.BONE.getDefaultStack());
        return true;
    }

    /* Dirty Method for Client */
    private static final Random random = new Random();
    private static final List<String> texts = Lists.newArrayList(
            "尝试一下!",
            "反射一下!",
            "阿萨姆一下!",
            "什么，怎么可以米线!",
            "我打现代收割机?真的假的?",
            "凤梨科技!",
            "凤梨凤梨大凤梨~",
            "大胆尝试!",
            "凤梨最好的朋友!",
            "聚光灯下!",
            "非人类!",
            "桌子下有非常多凤梨，而你，我的朋友，你一只都吃不到",
            "我有两只，给你零只"
    );
    private static final char[] code = "@#$%^&".toCharArray();

    private static int tickOfRender = 0;
    private static final int maxTickOfRender = 20 * 6;

    private static String textHolder = "";
    private static final StringBuilder textInRender = new StringBuilder();

    private Text getTooltipText() {
        if (textHolder.isEmpty()) textHolder = texts.get(random.nextInt(texts.size()));

        if (textInRender.length() < textHolder.length() || !textInRender.toString().equals(textHolder)) {
            if (++tickOfRender < 5) return Text.of(textInRender.toString());
            tickOfRender = 0;
            if (!textInRender.isEmpty()) {
                var pointer = textInRender.length() - 1;
                textInRender.deleteCharAt(pointer);
                textInRender.append(textHolder.charAt(pointer));
                if (textInRender.length() == textHolder.length()) {
                    return Text.of(textInRender.toString());
                }
            }
            textInRender.append(code[random.nextInt(code.length)]);
            return Text.of(textInRender.toString());
        } else if (++tickOfRender < maxTickOfRender) return Text.of(textInRender.toString());

        tickOfRender = 0;
        textHolder = "";
        textInRender.setLength(0);

        return Text.of("NaN");
    }
}
